import django_filters
from public.models import *
from distutils.util import strtobool


def systemlist(request):
    if request is None:
        return SystemsLists.objects.none()

    user = User.objects.get(username=request.user.username)
    if user.username == 'root':
        system = SystemsLists.objects.all()
    else:
        if user.groups.all().first():
            group = user.groups.all().first()
            system = group.systemslists_set.all()

    return system


BOOLEAN_CHOICES = (('', 'Any'), ('false', '否'), ('true', '是'),)


class HostFilter(django_filters.FilterSet):
    systemname = django_filters.ModelChoiceFilter(queryset=systemlist, empty_label='请选择', label="系统")
    servicename = django_filters.CharFilter(field_name='servicename',
                                            lookup_expr='icontains', label="服务")
    clustername = django_filters.CharFilter(field_name='clustername',
                                            lookup_expr='icontains', label="集群")
    hostname = django_filters.CharFilter(field_name='hostname',
                                         lookup_expr='icontains', label="服务器")
    hlocation = django_filters.CharFilter(field_name='location',
                                          lookup_expr='icontains', label="位置")
    ip = django_filters.CharFilter(field_name='ip',
                                   lookup_expr='icontains', label="IP")
    in4A = django_filters.TypedChoiceFilter(field_name='in4A',
                                            choices=BOOLEAN_CHOICES,
                                            coerce=strtobool, label="4A")
    manage = django_filters.TypedChoiceFilter(field_name='manage',
                                            choices=BOOLEAN_CHOICES,
                                            coerce=strtobool, label="批量管理")

    class Meta:
        model = HostsLists
        fields = {
        }


class HostinfoFilter(django_filters.FilterSet):
    systemname = django_filters.CharFilter(field_name='systemname',
                                           lookup_expr='icontains', label="系统")
    hostname = django_filters.CharFilter(field_name='hostname',
                                         lookup_expr='icontains', label="主机名")
    sship = django_filters.CharFilter(field_name='sship',
                                        lookup_expr='icontains', label="sship")

    class Meta:
        model = HostRes
        fields = {
        }


class SystemFilter(django_filters.FilterSet):
    systemname = django_filters.CharFilter(field_name='systemname',
                                           lookup_expr='icontains', label="系统")
    control_host = django_filters.CharFilter(field_name='control_host',
                                             lookup_expr='icontains', label="控制主机")

    class Meta:
        model = SystemsLists
        fields = {
        }


class PlaybookFilter(django_filters.FilterSet):
    playbook = django_filters.CharFilter(field_name='playbook',
                                         lookup_expr='icontains', label="playbook名")
    nickName = django_filters.CharFilter(field_name='nickName',
                                         lookup_expr='icontains', label="别名")
    author = django_filters.CharFilter(field_name='author__username',
                                         lookup_expr='icontains', label="作者")

    class Meta:
        model = Playbooks
        fields = {
        }


class SoftwareFilter(django_filters.FilterSet):
    software = django_filters.CharFilter(field_name='software',
                                         lookup_expr='icontains', label="软件名")

    class Meta:
        model = Softwares
        fields = {
        }


class ApplicationFilter(django_filters.FilterSet):
    module = django_filters.CharFilter(field_name='module',
                                       lookup_expr='icontains', label="模块名")
    system_name = django_filters.CharFilter(field_name='systemslists__systemname',
                                            lookup_expr='icontains', label="系统")

    ip = django_filters.CharFilter(field_name='hostslists__ip',
                                   lookup_expr='icontains', label="ip")

    class Meta:
        model = Applications
        fields = {
        }


class AnsibleRoleFilter(django_filters.FilterSet):
    ansiblerole = django_filters.CharFilter(field_name='ansiblerole',
                                            lookup_expr='icontains', label="ansiblerole名")

    class Meta:
        model = AnsibleRole
        fields = {
        }


class AnsibleRoleTasksFilter(django_filters.FilterSet):
    ansiblerole = django_filters.CharFilter(field_name='ansiblerole',
                                            lookup_expr='icontains', label="ansiblerole名")

    class Meta:
        model = AnsibleRoleTasks
        fields = {
        }


class AnsibleTaskFilter(django_filters.FilterSet):
    SystemName = django_filters.CharFilter(field_name='SystemName',
                                           lookup_expr='icontains', label="系统")
    TaskUser = django_filters.CharFilter(field_name='TaskUser__username',
                                         lookup_expr='icontains', label="用户")
    TaskName = django_filters.CharFilter(field_name='TaskName',
                                         lookup_expr='icontains', label="TaskName")
    playbook = django_filters.CharFilter(field_name='playbook',
                                         lookup_expr='icontains', label="playbook")

    class Meta:
        model = AnsibleTasks
        fields = {
        }


class AssetsFilter(django_filters.FilterSet):
    number = django_filters.CharFilter(field_name='number',
                                           lookup_expr='icontains', label="资产编号")
    label = django_filters.CharFilter(field_name='label',
                                         lookup_expr='icontains', label="资产标签")
    name = django_filters.CharFilter(field_name='name',
                                         lookup_expr='icontains', label="资产名称")
    employeename = django_filters.CharFilter(field_name='employeename',
                                         lookup_expr='icontains', label="员工姓名")
    class Meta:
        model = Assets
        fields = {
        }
